<?php
require_once(DP_BASE_DIR."/modules/masterdata/mdunit.class.php");
$unit_id = intval( dPgetParam( $_GET, 'unit_id', 0 ) );

// check permissions for this record
/*
$perms =& $AppUI->acl();
if (! ($canEdit = $perms->checkModuleItem( 'currencys', 'edit', $currency_id )) ) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}
*/

// load the record data
$msg = '';
$row = new CMdUnit();

$canDelete = $row->canDelete( $msg, $currency_id );
if($msg == $AppUI->_('UnitsDeleteUserError', UI_OUTPUT_JS)) {
	$userDeleteProtect=true;
}

if ($view) {
	$view = ' readonly="true"';
}
else {
	unset($view);
}

if (!$row->load( $unit_id ) && $unit_id > 0) {
	$AppUI->setMsg( 'Unit' );
	$AppUI->setMsg( 'invalidID', UI_MSG_ERROR, true );
	$AppUI->redirect();
}

$status_list=null;
$type_list=null;

$q = new DBQuery();
$q->addTable('md_unit');
$q->addQuery('unit_code');
if (isset($row->unit_code)) {
	$q->addWhere("unit_id != $row->unit_id");
}
$quoteCodeList = $q->loadColumn();
$qCL = '["' . implode('", "', $quoteCodeList) . '"]';


if (!$view) {
	require_once(DP_BASE_DIR."/modules/masterdata/mdstatus.class.php");
	$status_list = getStatusList($typeUnit);
	require_once(DP_BASE_DIR."/modules/masterdata/mdtype.class.php");
	$type_list = getTypeList($typeUnit);
?>
<form name="MdUnitForm" action="?m=masterdata" method="post">
<input type="hidden" name="dosql" value="mdunit_do_aed" />
<input type="hidden" name="unit_id" value="<?=$unit_id?>" />
<?}?>
<table border="0" cellpadding="8" cellspacing="0" width="400px">
<tr>
<td colspan="2" bgcolor="#E0E0E0"><b><?=$AppUI->_('Unit Information')?></td>
</tr>
<tr>
	<td colspan="2">
		<table border="0" cellpadding="0" cellspacing="4" width="100%">
		<tr>
			<td align="right">* <?=$AppUI->_('Code')?></td>
			<td>
				<input type="text" class="text" name="unit_code" id="unit_code"
				value="<?=@$row->unit_code?>" maxlength="10" size="10" style="width:98%" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right">* <?=$AppUI->_('Name')?></td>
			<td>
				<input type="text" class="text" name="unit_name" id="unit_name"
					value="<?=@$row->unit_name?>" maxlength="150" style="width:98%" <?=$view?>/>
			</td>
		</tr>
		<? if (!$view) { ?>
		<tr>
			<td align="right">* <?=$AppUI->_('Status')?></td>
			<td>
				<? echo arraySelect($status_list,'unit_status', 'id="unit_status" style="width: 150px;" title="Status :: Status is required"',@$row->unit_status)?>
			</td>
		</tr>
		<tr>
			<td align="right">* <?=$AppUI->_('Type')?></td>
			<td>
				<? echo arraySelect($type_list,'unit_type','id="unit_type" style="width: 150px;" title="Type :: Type is required"',@$row->unit_type)?>
			</td>
		</tr>

		<? }else { ?>
		<tr>
			<td align="right"><?=$AppUI->_('Status')?></td>
			<td><? echo @$row->getStatusName();?> </td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Type')?></td>
			<td><? echo @$row->getTypeName();?> </td>
		</tr>
		<? } ?>
		<tr>
			<td align="right"><?=$AppUI->_('Active')?></td>
			<td>
				<input type="checkbox" name="unit_active"
				value="<?=@$row->unit_active?>" maxlength="50" <?=$view?> <?= (@$row->unit_active == 1) ? "checked":""?>/>
			</td>
		</tr>
		</table>
	</td>
</tr>
<tr>
	<td align="center" colspan="2" style="border-top: 1px solid #E0E0E0">
	<? if (!$view) { ?>
		<input type="submit" class="button" value="<?=$AppUI->_('Save')?>"/>
		<input type="button" class="button" value="<?=$AppUI->_('Cancel')?>" onClick="doCloseUnit()"/>
	<? } else { ?>
		<input type="button" class="button" value="<?=$AppUI->_('Close')?>" onClick="doCloseUnit()"/>
	<? } ?>
	</td>
</tr>
</table>
</form>
<div id="validation_rules" style="visibility: hidden; display: none">
	var fUnitCode = new LiveValidation('unit_code');
	fUnitCode.add( Validate.Presence );
	fUnitCode.add( Validate.Exclusion , {within: <?=$qCL?>});

	var fUnitName = new LiveValidation('unit_name');
	fUnitName.add( Validate.Presence );

	var fUnitType = new LiveValidation('unit_type');
	fUnitType.add( Validate.Presence );

	var fUnitStatus = new LiveValidation('unit_status');
	fUnitStatus.add( Validate.Presence );

	new Tips($$('.text'));
</div>